var REQUEST={
	getBook:function(id){
		return $.getJSON('http://userbe.oss-cn-shanghai.aliyuncs.com/book_loc'+id)
	},
	getContent:function(data){
		return $.ajax({
			url:'/Readdoc/getContent',
			data:data,
			type:'post',
			dataType:'json'
		})
	}
}

var GET=function(id){
	this.container=[];
	this.content={};
	this.id=id;
	this.init(id)

}
GET.prototype={
	init:function(id){
		var that=this;
		REQUEST.getBook(id).done(function(res){
			that.data=res;
			that.start();
		})
	},
	start:function(){
		var data=this.data;
		for(var i in data){
			this.container.push(data[i])
		}
		this.curr=0;

		this.getContent(this.data[1])
	},
	getContent:function(info){
		var that=this;

		var par={
			bookid:this.id,
			id:info.b,
			gid:info.t!=1?info.g:0
		};

		REQUEST.getContent(par).done(function(res){
			that.curr++;
			
			if(info.t==1){
				that.data[info.b].content=res.data[0];
			}else{
				that.data[info.b].content=res.data[0];
				that.data[info.g].content=res.data[1];
			}
			if(that.curr==that.container.length){
				var data=that.data;
				for(var v in data){
					that.content[v]={
						bid:data[v].b,
						gid:data[v].g,
						sid:data[v].s,
						content:data[v].content,
						pid:data[v].p,
						type:data[v].t
					}
				}
				array=that.content;
				console.log(JSON.stringify(array));
			}else{
				that.getContent(that.container[that.curr])
			}
		})
	}
}

module.exports.getContent=GET;